//
//  AuthodExpresserViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/10/22.
//  Copyright © 2015年 xingrong. All rights reserved.
//

#import "AuthodExpresserViewController.h"
#import "AuthodExpresserCell.h"
#import "SocketRequest.h"
#import "Singleton.h"
#import "ExpresserDBModel.h"

@interface AuthodExpresserViewController ()<SocketRequestDelegate> {
    SocketRequest *_socketRequest;
    NSMutableArray *_authodList;
}

@end

@implementation AuthodExpresserViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"已授权快递员(0)";
        _authodList = [NSMutableArray arrayWithCapacity:1];
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    [self getAuthodExpressListSocket];
}

- (void)getAuthodExpressListSocket {
    [self.tableView makeLoading];
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STO7"]];
    [mutableData appendBytes:[SocketMethod checkLegal:[Singleton shareInstance].userMessage.websiteId] length:64];
    [_socketRequest addSocketData:mutableData SocketTag:1];
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    [self.tableView makeNone];
    if ([code isEqualToString:SuccessSocket]) {
        NSString *content = [SocketMethod stringDecryHexData:data];
        NSArray *array = [content componentsSeparatedByString:@"|"];
        for (NSString *string in array) {
            if (string.length > 0) {
                ExpresserDBModel *expresser = [[ExpresserDBModel alloc] initWithAuthodString:string];
                [_authodList addObject:expresser];
            }
        }
        if (_authodList.count <= 0) {
            [self.tableView makeNoAuthodExpresserTip];
        }
        self.title = [NSString stringWithFormat:@"已授权快递员(%d)",(int)_authodList.count];
        [self.tableView reloadData];
    } else {
        [self.tableView makeTip:msg];
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    [self.tableView makeTip:msg Title:@"重新加载" Target:self Selector:@selector(getAuthodExpressListSocket)];
    [DSToast showText:msg InView:self.view];
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _authodList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentfier = @"AuthodExpresserCell";
    AuthodExpresserCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentfier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"AuthodExpresserCell" owner:self options:nil] lastObject];
    }
    ExpresserDBModel *expresser = [_authodList objectAtIndex:indexPath.row];
    [cell reloadAuthodExpresserCell:expresser];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ExpresserDBModel *expresser = [_authodList objectAtIndex:indexPath.row];
    if (expresser.money >= 0) {
        if ([_delegate respondsToSelector:@selector(didFinishSelectedAuthodExpresser:)]) {
            [_delegate didFinishSelectedAuthodExpresser:expresser];
        }
        [self performSelector:@selector(backLastViewController) withObject:nil afterDelay:0.5];
    } else {
        [DSToast showText:@"快递员账号欠费啦，赶紧叫他充值吧！" InView:self.view];
    }
}

@end
